#include <stdio.h>
#include "item_define.h"
#include "list.h"

static void show(Item item);
inline void eatline();
int main()
{
	Item temp;
	List movies;
	createList(&movies);
	if (isFull(&movies)){
		fprintf(stderr,"malloc error!\n");
		return -1;
	}

	printf("enter the title of movie <empty to quit>: ");
	while(fgets(temp.title,TSIZE, stdin) != NULL && temp.title[0] != '\0'){
		printf("enter the rate <0--10>: ");
		int ret = scanf("%d",&temp.rating);
        if (ret < 0) {
            return -1;
        }
		eatline();
		addItem(&movies,temp);

		if (isFull(&movies)){
			printf("The movie list is full\n");
			destroyList(&movies);
			return -1;
		}
		printf("enter the title of movie <empty to quit>: ");
	}

	printf("There are %d movies list:\n",list_length(&movies));
	traverse(&movies,show);

	destroyList(&movies);

	return 0;
}

static void show(Item item)
{
	printf("%s: %d\n",item.title,item.rating);
}

inline void eatline()
{
	while(getchar() != '\n')
		continue;
}

